package com.uchedao.vehicledetection.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.uchedao.mybaselibrary.utils.LogUtil;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.global.GlobalInfo;
import com.uchedao.vehicledetection.inf.InspectedCarSourceSubmit;
import com.uchedao.vehicledetection.model.Draft;
import com.uchedao.vehicledetection.model.InspectedCarSourceEntity;

import java.util.List;

/**
 * 已验车源－－适配器
 * Created by hang on 2015/9/14.
 */
public class InspectedCarSourceAdapter extends CommonAdapter<InspectedCarSourceEntity> {
    InspectedCarSourceSubmit sourceSubmit;

    public InspectedCarSourceAdapter(Context context, List<InspectedCarSourceEntity> data, InspectedCarSourceSubmit sourceSubmit) {
        super(context, data, R.layout.item_inspected_car_source);
        this.sourceSubmit = sourceSubmit;
    }

    @Override
    public void conver(ViewHolder holder, InspectedCarSourceEntity item, int position) {
        holder.setText(R.id.tvCheckedCarItemTitle, item.getTitle());
        holder.setText(R.id.tvCheckedCarItemNumber, item.getCar_number());
        holder.setText(R.id.tvCheckedCarItemVin, item.getVin());
        holder.setText(R.id.tvCheckedCarItemDate, item.getCreated_at());
        holder.setText(R.id.tvCheckedCarItemStatus, item.getStatus_cn());
        holder.setText(R.id.tv_check_result, "通过");
//        '-1' => '草稿', '0' => '未审核','1' => '上架','2' => '下架', '3' => '删除'
//        ,'4' => '待验车','5' => '无法验车','6' => '成交','7' => '待处理',
        if (item.getStatus() == -1) {
            if (item.getCheck_result() == 1) {
                holder.getView(R.id.button).setVisibility(View.VISIBLE);
                holder.getView(R.id.button).setTag(item.getCar_number());
                holder.getView(R.id.button).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (sourceSubmit == null) {
                            LogUtil.e("sourceSubmit == null");
                            return;
                        }
                        String car_plate = v.getTag().toString();
                        LogUtil.e("car_plate == " + car_plate);
                        if (TextUtils.isEmpty(car_plate)) {
                            sourceSubmit.showError("车牌数据错误！");
                        } else {
                            Draft draft = GlobalInfo.dbHelper.getDraft(car_plate);
                            if (draft == null) {
                                LogUtil.e("draft == null " + car_plate);
                                sourceSubmit.showError(car_plate + "检测信息已被删除或非本机数据！");
                            } else {
                                LogUtil.e("sourceSubmit == " + draft.car_plate);
                                sourceSubmit.submit(draft);
                            }
                        }
                    }
                });
            } else {
                holder.getView(R.id.button).setVisibility(View.GONE);
                holder.setText(R.id.tv_check_result, "不通过");
            }
        } else {
            holder.getView(R.id.button).setVisibility(View.GONE);
        }
    }
}
